package controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import model.Game;
import model.User;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import snake.GamePackage;
import snake.GamePackage.GameStatus;
import utils.LoggedUser;

@Controller
public class MainController
{

	@RequestMapping(value = "getAllOpenedGames", method = RequestMethod.POST, headers = "Accept=application/json")
	public @ResponseBody List<Game> getAllOpenedGames(HttpServletRequest request)
	{
		List<Game> openedGames = null;

		openedGames = (List<Game>) request.getServletContext().getAttribute("games");

		// System.out.println(openedGames);

		return openedGames;

	}

	@RequestMapping("/createGame.do")
	public String createGame(HttpServletRequest request)
	{
		if (LoggedUser.isLogged(request.getSession()))
		{
			ArrayList<Game> games = (ArrayList<Game>) request.getServletContext().getAttribute("games");

			if (games == null)
			{
				request.getServletContext().setAttribute("games", new ArrayList<Game>());
				games = (ArrayList<Game>) request.getServletContext().getAttribute("games");
			}

			int newId = games.size();
			for (int i = 0; i < games.size(); i++)
			{

				if (games.get(i).getPlayerList().get(0) == LoggedUser.getUser(request.getSession()))
				{
					newId = games.get(i).getId();
					games.remove(i);
				}

			}

			Game game = new Game();
			game.setDescription("Snake game");
			game.setScoreLimit(10);
			game.setNrOfPlayers(2);
			game.setId(newId);
			game.setPlayerList(new ArrayList<User>());
			User user = LoggedUser.getUser(request.getSession());
			game.getPlayerList().add(user);
			games.add(game);

			request.getSession().setAttribute("gameId", game.getId());

			return "lobby";
		}

		return "index";
	}

	@RequestMapping("/updateLobby.do")
	public @ResponseBody String updateLobby(HttpServletRequest request)
	{
		int newNrOfPlayers = Integer.parseInt(request.getParameter("nrOfPlayers"));
		int scoreLimit=10;
		if(request.getParameter("scoreLimit")!="")
		{
			scoreLimit = Integer.parseInt(request.getParameter("scoreLimit"));
		}
		String description = request.getParameter("description");

		int gameId = (int) request.getSession().getAttribute("gameId");

		List<Game> games = (List<Game>) request.getServletContext().getAttribute("games");
		games.get(gameId).setNrOfPlayers(newNrOfPlayers);
		games.get(gameId).setScoreLimit(scoreLimit);
		games.get(gameId).setDescription(description);

		return "ok";

	}

	@RequestMapping("/refreshLobby.do")
	public @ResponseBody Game refreshLobby(HttpServletRequest request)
	{

		int gameId = Integer.parseInt(request.getParameter("gameId"));

		List<Game> games = (List<Game>) request.getServletContext().getAttribute("games");

		Game game = null;

		for (Game g : games)
		{
			if (g.getId() == gameId)
			{
				game = g;
				break;
			}
		}

		if (game != null)
		{
			List<GamePackage> games2 = (List<GamePackage>) request.getServletContext().getAttribute("startedGames");
			if (games2 != null)
			{

				for (GamePackage game2 : games2)
				{
					if (game2.getGameId() == gameId && game2.getGameStatus() != GameStatus.FINISHED)
					{
						game.setStartTime(new Date());
						break;
					}
				}

			}

		}

		return game;

	}

	@RequestMapping("/leaveLobby.do")
	public String leaveLobby(HttpServletRequest request)
	{
		if (LoggedUser.isLogged(request.getSession()))
		{
			int gameId = (Integer) request.getSession().getAttribute("gameId");
			int gameIndex=-1;

			List<Game> games = (List<Game>) request.getServletContext().getAttribute("games");

			List<User> playerList = null;
			for (Game g : games)
			{
				if (g.getId() == gameId)
				{
					gameIndex=games.indexOf(g);
					playerList = g.getPlayerList();
				}
			}

			if (playerList != null)
			{
				playerList = games.get(gameIndex).getPlayerList();

				User currUser = LoggedUser.getUser(request.getSession());
				for (int i = 0; i < playerList.size(); i++)
				{

					if (playerList.get(i) == currUser)
					{
						if (i == 0)
						{
							games.remove(gameIndex);
						} else
						{
							playerList.remove(i);
						}
					}

				}

				request.getSession().removeAttribute("gameId");

				return "mainPage";
			} else
			{
				return "mainPage";
			}
		}
		return "index";
	}

	@RequestMapping("join")
	public String join(HttpServletRequest request)
	{
		if (LoggedUser.isLogged(request.getSession()))
		{
			int gameId = Integer.parseInt(request.getParameter("id"));
			request.getSession().setAttribute("gameId", gameId);
			ArrayList<Game> games = (ArrayList<Game>) request.getServletContext().getAttribute("games");

			if (games != null)
			{
				Game currentGame = games.get(gameId);
				User user = LoggedUser.getUser(request.getSession());

				if (!currentGame.getPlayerList().contains(user) && currentGame.getNrOfPlayers() != currentGame.getPlayerList().size())
				{
					currentGame.getPlayerList().add(user);
					return "lobby";
				}

			}

			return "mainPage";
		}
		return "index";
	}

}
